<?php
defined('_XR_EXEC') or die();
/**
 * test module
 */
class source_model extends model
{ 
	public function __construct() {
		parent::__construct();
	}

	/* 用户行为日志记录 */
	public function logs($source_id,$user_id,$logs=''){
		$db = factory::getdbo();
		return $db->insert('source_logs',array(
			'source_id'=>intval($source_id),
			'user_id'=>intval($user_id),
			'dates'=>'now()',
			'clientip'=>ip::client(),
			'serverip'=>ip::server(),
			'logs'=>$logs
		));
	}
	/* 溯源建立 */
	public function add($sendpi_id){
		$db = factory::getdbo();
		$source_id = $db->insert('source',array(
			'sendpi_id'=>$sendpi_id,
			'created_user_id'=>factory::module('admin')->getuserid(),
			'created'=>'now()', 
			'updated_user_id'=>factory::module('admin')->getuserid(),
			'updated'=>'now()'
		));
		if(!$source_id){
			return false;
		}
		$this->logs($source_id,factory::module('admin')->getuserid(),'溯源信息建立');
		return $source_id;
	}
	/* 溯源更新-根据配送批次ID 进行更新 */
	public function update($sendpi_id){
		$db = factory::getdbo();
		$rs_source = $db->loadassoc('select * from t_source where sendpi_id="'.$sendpi_id.'" and deleted=0 ');
		if(!$rs_source){
			return false;
		}
		# 查询当前处理批次中所有的人员和蔬菜
		$users = array_keys($db->loadassoclist('select distinct(user_id) from t_send where sendpi_id="'.$sendpi_id.'" ','user_id'));
		$veges = array_keys($db->loadassoclist('select distinct(vege_id) from t_send_vege where sendpi_id="'.$sendpi_id.'" ','vege_id'));
		#查询出当前处理批次已经存在的蔬菜和人员
		$s_users = array_keys($db->loadassoclist('select distinct(user_id) from t_source_user where sendpi_id="'.$sendpi_id.'" ','user_id'));
		$s_veges = array_keys($db->loadassoclist('select distinct(vege_id) from t_source_vege where sendpi_id="'.$sendpi_id.'" ','vege_id'));
		
	
		#检查是否有添加
		foreach($users as $v){
			$b_add = true;
			foreach($s_users as $val){
				if($v == $val){
					$b_add = false;
					break;
				}
			}
			if($b_add){
				$db->insert('source_user',array(
					'source_id'=>$rs_source['id'],
					'sendpi_id'=>$sendpi_id,
					'user_id'=>$v
				));
			}
		}
		#检查是否有删除人员
		$s_users = array_keys($db->loadassoclist('select distinct(user_id) from t_source_user where sendpi_id="'.$sendpi_id.'" ','user_id'));
		foreach($s_users as $v){
			$b_del = true;
			foreach($users as $val){
				if($v == $val){
					$b_del = false;
				}
			}
			if($b_del){
				$db->query('delete from t_source_user where sendpi_id="'.$sendpi_id.'" and user_id="'.$v.'" ');
			}
		}
	 
		#检查是否有添加
		foreach($veges as $v){
			$b_add = true;
			foreach($s_veges as $val){
				if($v == $val){
					$b_add = false;
					break;
				}
			}
			if($b_add){
				$db->insert('source_vege',array(
					'source_id'=>$rs_source['id'],
					'sendpi_id'=>$sendpi_id,
					'vege_id'=>$v
				));
			}
		}
		#检查是否有删除人员
		$s_veges = array_keys($db->loadassoclist('select distinct(vege_id) from t_source_vege where sendpi_id="'.$sendpi_id.'" ','vege_id'));
		foreach($s_veges as $v){
			$b_del = true;
			foreach($veges as $val){
				if($v == $val){
					$b_del = false;
				}
			}
			if($b_del){
				$db->query('delete from t_source_vege where sendpi_id="'.$sendpi_id.'" and vege_id="'.$v.'" ');
			}
		}
		# 进行状态查询，各个阶段的检查!!!!!!!!!!
		# 供应阶段
		$rs_prosource = $db->loadassoclist('
			select 
				id,
				pro_base,
				pro_height,
				pro_picktime,
				pro_pickman,
				pro_testman,
				pro_plantime
			from t_source_vege 
			where source_id="'.$rs_source['id'].'" 
		');
		$pro_fields = array_keys($db->loadassoclist('select * from t_field where who="1" order by show_index asc ','id'));
		
		foreach($rs_prosource as $k=>$v){
			foreach($pro_fields as $vv){
				$tmp = $db->loadresult('select field_val from t_field_vege_val where source_vege_id="'.$v['id'].'" and field_id="'.$vv.'"  ');
				$rs_prosource[$k][$vv] = $tmp==false?"":$tmp;
			}
		}
		$pro_flag = '1';
		foreach($rs_prosource as $val){
			foreach($val as $yanval){
				if($yanval == ''){
					$pro_flag = '0';
					break;
				}
			}
		}
		$db->update('source',array(
			'id'=>$rs_source['id'],
			'pro_flag'=>$pro_flag
		));
		 
		# 生产阶段
		$rs_makesource = $db->loadassoclist('
			select 
				id,
				make_intime, 
				make_time, 
				make_testman
			from t_source_vege 
			where source_id="'.$rs_source['id'].'" 
		');
		$make_fields = array_keys($db->loadassoclist('select * from t_field where who="2" order by show_index asc ','id'));
		foreach($rs_makesource as $k=>$v){
			foreach($make_fields as $vv){
				$tmp = $db->loadresult('select field_val from t_field_vege_val where source_vege_id="'.$v['id'].'" and field_id="'.$vv.'"  ');
				$rs_makesource[$k][$vv] = $tmp==false?"":$tmp;
			}
		}
		$make_flag = '1';
		foreach($rs_makesource as $val){
			foreach($val as $yanval){
				if($yanval == ''){
					$make_flag = '0';
					break;
				}
			}
		}
		$db->update('source',array(
			'id'=>$rs_source['id'],
			'make_flag'=>$make_flag
		));
				 
		# 配送阶段
		$rs_sendsource = $db->loadassoclist('
			select 
				id,
				send_outtime, 
				send_man
			from t_source_user 
			where source_id="'.$rs_source['id'].'" 
		');
		$send_fields = array_keys($db->loadassoclist('select * from t_field where who="3" order by show_index asc ','id'));
		foreach($rs_sendsource as $k=>$v){
			foreach($send_fields as $vv){
				$tmp = $db->loadresult('select field_val from t_field_user_val where source_user_id="'.$v['id'].'" and field_id="'.$vv.'"  ');
				$rs_sendsource[$k][$vv] = $tmp==false?"":$tmp;
			}
		}
		$send_flag = '1';
		foreach($rs_sendsource as $val){
			foreach($val as $yanval){
				if($yanval == ''){
					$send_flag = '0';
					break;
				}
			}
		}
		$db->update('source',array(
			'id'=>$rs_source['id'],
			'send_flag'=>$send_flag
		));
		$rs_nowsource = $db->loadassoc('select * from t_source where id="'.$rs_source['id'].'" ');
		if($rs_nowsource['pro_flag'] == "1" && $rs_nowsource['make_flag'] == "1" && $rs_nowsource['send_flag'] == "1"){
			$db->update('source',array(
				'id'=>$rs_source['id'],
				'state'=>"1"
			));
		}else{
			$db->update('source',array(
				'id'=>$rs_source['id'],
				'state'=>"0"
			));
		}
		return true;
	}
	
	
	# 供应阶段的自定义字段
	public function getField($who){
		$db = factory::getDBO();
		if($who != '1' && $who != '2' && $who != '3'){
			$who = '1';
		}
		$fields = $db->loadassoclist('select * from t_field where who="'.$who.'" order by show_index asc ');
		foreach($fields as &$v){
			if($v['field_type'] == 'slt' || $v['field_type'] == 'mslt'){ 
				$v['option'] = array_keys( $db->loadassoclist('select val from t_field_option where field_id="'.$v['id'].'" order by show_index asc ','val') );
			}
		}
		return $fields;
	}
	 
	# 蔬菜溯源
	public function getSourceVege($source_id){

		$db = factory::getDBO();

		$rs_source_vege = $db->loadassoclist('
			select 
				a.id,
				a.vege_id,
				b.name as vege_name,
				c.id as vegetype_id,
				c.type_name as vegetype_name,
				c.pro_base as dft_pro_base,
				c.pro_height as dft_pro_height,
				c.pro_pickman as dft_pro_pickman,
				c.pro_testman as dft_pro_testman,
				c.make_testman as dft_make_testman,
				a.pro_base,
				a.pro_height,
				IFNULL(a.pro_picktime,"") as pro_picktime,
				a.pro_pickman,
				a.pro_testman,				
				IFNULL(a.pro_plantime,"") as pro_plantime,
				IFNULL(a.make_intime,"")as make_intime,
				IFNULL(a.make_time,"") as make_time,
				a.make_testman
			from t_source_vege  a
			left join t_vege b on a.vege_id=b.id
			left join t_vegetype c on b.type_id=c.id
			where a.source_id="'.$source_id.'" 
		');
	  # 供应阶段的自定义字段
		$pro_fields = $this->getField('1');
	  # 合并字段进入到集合里
		foreach($rs_source_vege as $k=>$v){
			foreach($pro_fields as $vv){
				$tmp = $db->loadresult('select field_val from t_field_vege_val where source_vege_id="'.$v['id'].'" and field_id="'.$vv['id'].'"  ');
				$rs_source_vege[$k][$vv['id']] = $tmp==false?"":$tmp;
			} 
		}
	  # 生产阶段的自定义字段
		$make_fields = $this->getField('2');
	  # 合并字段进入到集合里
		foreach($rs_source_vege as $k=>$v){
			foreach($make_fields as $vv){
				$tmp = $db->loadresult('select field_val from t_field_vege_val where source_vege_id="'.$v['id'].'" and field_id="'.$vv['id'].'"  ');
				$rs_source_vege[$k][$vv['id']] = $tmp==false?"":$tmp;
			} 
		}
		return $rs_source_vege;
	}
	
	
	# 蔬菜溯源
	public function getSourceUser($source_id){
		$db = factory::getDBO();
		$rs_source_user = $db->loadassoclist('
			select 
				a.id,
				a.user_id,
				b.name as name,
				b.username as username,
				c.send_man as dft_send_man,
				c.val as zone,
				ifnull(a.send_outtime,"") as send_outtime,
				a.send_man
			from t_source_user  a
			left join t_user b on a.user_id=b.id
			left join t_list_zone c on b.zone=c.id
			where a.source_id="'.$source_id.'" 
		');	
	  # 配送阶段的自定义字段
		$send_fields = $this->getField('3');	
	  # 合并字段进入到集合里
		foreach($rs_source_user as $k=>$v){
			foreach($send_fields as $vv){
				$tmp = $db->loadresult('select field_val from t_field_user_val where source_user_id="'.$v['id'].'" and field_id="'.$vv['id'].'"  ');
				$rs_source_user[$k][$vv['id']] = $tmp==false?"":$tmp;
			} 
		}
		return $rs_source_user;
	}
	
	
	
	# 会员溯源信息
	public function getSourceByUserSendpi($user_id,$sendpi_id){
		
		$db = factory::getDBO();
	  # 溯源信息
		$rs_source = $db->loadassoc('select 
					a.*
				from t_source a
				left join t_sendpi b on a.sendpi_id=b.id
				where a.sendpi_id="'.$sendpi_id.'" and a.deleted=0 and b.deleted=0 ');
		if(!$rs_source){
			return array();
		}
		$source_id=$rs_source['id'];
		/* 当前用户所选择的菜品种类 */
		$veges = $db->loadassoclist('
			select 
			distinct(a.vege_id) 
			from t_send_vege a  
			left join t_send b on a.send_id=b.id
			where a.sendpi_id="'.$sendpi_id.'" and b.user_id="'.$user_id.'"
		','vege_id');
		if(count($veges) <=0){
			return array();
		}
		$veges = join(',',array_keys($veges));
		$rs_source_vege = $db->loadassoclist('
			select 
				a.id,
				a.vege_id,
				b.name as vege_name,
				c.type_name as vegetype_name,
				a.pro_base,
				a.pro_height,
				IFNULL(a.pro_picktime,"") as pro_picktime,
				a.pro_pickman,
				a.pro_testman,
				IFNULL(a.pro_plantime,"") as pro_plantime,
				IFNULL(a.make_intime,"") as make_intime,
				IFNULL(a.make_time,"") as make_time,
				a.make_testman
			from t_source_vege  a
			left join t_vege b on a.vege_id=b.id
			left join t_vegetype c on b.type_id=c.id
			where a.source_id="'.$source_id.'" and a.vege_id in ('.$veges.')
		');
	  # 供应阶段的自定义字段
		$pro_fields = $this->getField('1');
	  # 合并字段进入到集合里
		foreach($rs_source_vege as $k=>$v){
			foreach($pro_fields as $vv){
				$tmp = $db->loadresult('select field_val from t_field_vege_val where source_vege_id="'.$v['id'].'" and field_id="'.$vv['id'].'"  ');
				$rs_source_vege[$k][$vv['id']] = $tmp==false?"":$tmp;
			} 
		}
	  # 生产阶段的自定义字段
		$make_fields = $this->getField('2');
	  # 合并字段进入到集合里
		foreach($rs_source_vege as $k=>$v){
			foreach($make_fields as $vv){
				$tmp = $db->loadresult('select field_val from t_field_vege_val where source_vege_id="'.$v['id'].'" and field_id="'.$vv['id'].'"  ');
				$rs_source_vege[$k][$vv['id']] = $tmp==false?"":$tmp;
			} 
		}
		
		
		# 查找该用户配送溯源
		$rs_source_user = $db->loadassoc('
			select 
				a.id,
				ifnull(a.send_outtime,"") as send_outtime,
				a.send_man
			from t_source_user  a
			where a.source_id="'.$source_id.'" and a.user_id="'.$user_id.'"
		');	
	  # 配送阶段的自定义字段
		$send_fields = $this->getField('3');	
	  # 合并字段进入到集合里
		foreach($send_fields as $vv){
			$tmp = $db->loadresult('select field_val from t_field_user_val where source_user_id="'.$rs_source_user['id'].'" and field_id="'.$vv['id'].'"  ');
			$rs_source_user[$vv['id']] = $tmp==false?"":$tmp;
		} 
		# 合并
		foreach($rs_source_vege as $k=>$v){
			$rs_source_vege[$k]['send_outtime'] = $rs_source_user['send_outtime'];
			$rs_source_vege[$k]['send_man'] = $rs_source_user['send_man'];
			foreach($send_fields as $vv){
				$rs_source_vege[$k][$vv['id']] = $rs_source_user[$vv['id']];
			} 
		}
		return $rs_source_vege;
	}
	
}

?>